package com.future.mayi.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.future.baselib.utils.CommonUtils;
import com.future.mayi.R;
import com.future.mayi.model.CateTypeModel;

import java.util.ArrayList;
import java.util.List;

public class TypeItemView extends LinearLayout implements View.OnClickListener {
    private Context mContext;

    private TextView textView;
    private TextView tvAllType;
    private LinearLayout llType;
    private List<TextView> textViewList;
    private LayoutInflater mInflater;
    private List<Integer> list;
    private CateTypeModel cateTypeModel;

    private int maxSelect = 0;

    public TypeItemView(Context context) {
        super(context);
        init(context);
    }

    public TypeItemView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public TypeItemView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private TypeItemViewListener listener;


    public interface TypeItemViewListener {
        void onTypeChange();
    }

    int itemW;

    private void init(Context context) {
        mContext = context;
        itemW = (CommonUtils.getScreenSize(mContext)[0] - CommonUtils.dip2px(mContext, 8.5f * 4 + 60 + 1.5f + 10)) / 4;
        View.inflate(context, R.layout.layout_type_item, this);
        textView = findViewById(R.id.tv_name);
        tvAllType = findViewById(R.id.tv_all_type);
        tvAllType.setTag(false);
        llType = findViewById(R.id.ll_type);
        mInflater = LayoutInflater.from(mContext);
    }

    public void setData(CateTypeModel model) {
        if (model == null) return;
        cateTypeModel = model;
        list = new ArrayList<>();
        textView.setText(TextUtils.isEmpty(model.getTitle()) ? "" : model.getTitle());
        textViewList = new ArrayList<>();
        if (llType.getChildCount() > 0)
            llType.removeAllViews();
        TextView tv;
        if (model.getCate_son() == null || model.getCate_son().size() == 0) return;

        LinearLayout linearLayout = null;
        for (int i = 0; i < model.getCate_son().size(); i++) {
            if (i % 4 == 0) {
                linearLayout = new LinearLayout(mContext);
                linearLayout.setOrientation(HORIZONTAL);
                linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                llType.addView(linearLayout);
            }
            tv = (TextView) mInflater.inflate(R.layout.layout_type_item_text, llType, false);
            tv.setText(model.getCate_son().get(i).getTitle());
            tv.setTag(i);
            tv.setOnClickListener(this);
            if (linearLayout != null)
                linearLayout.addView(tv);
            textViewList.add(tv);


            LinearLayout.LayoutParams layoutParams = (LayoutParams) tv.getLayoutParams();
            layoutParams.width = itemW;
        }


        if (llType.getChildCount() > 3) {
            tvAllType.setVisibility(VISIBLE);

            {
                ViewGroup.LayoutParams layoutParams = llType.getLayoutParams();
                layoutParams.height = CommonUtils.dip2px(mContext, 38 * 3);
                llType.setLayoutParams(layoutParams);
            }

            tvAllType.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean open = (!(boolean) v.getTag());
                    ViewGroup.LayoutParams layoutParams = llType.getLayoutParams();
                    if (open)
                        layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                    else
                        layoutParams.height = CommonUtils.dip2px(mContext, 38 * 3);
                    llType.setLayoutParams(layoutParams);
                    tvAllType.setText(open ? "收起" : "全部");
                    Drawable drawable = getResources().getDrawable(open ? R.drawable.img_shang : R.drawable.img_xia);
                    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                    tvAllType.setCompoundDrawables(null, null, drawable, null);
                    tvAllType.setTag(open);
                }
            });
        } else {
            tvAllType.setVisibility(GONE);
        }


    }

    @Override
    public void onClick(View view) {
        int position = (int) view.getTag();
        selectPoition(view, position);
    }


    private void selectPoition(View view, int position) {
        int id = cateTypeModel.getCate_son().get(position).getCate_id();
        TextView textView;
        if (view == null) {
            textView = textViewList.get(position);
        } else {
            textView = (TextView) view;
        }
        boolean exist = false;
        int index = -1;

        if (list == null) list = new ArrayList<>();

        if (list.size() > 0)
            for (int i = 0; i < list.size(); i++) {
                int a = list.get(i);
                if (a == id) {
                    exist = true;
                    index = i;
                }
            }

        if (exist) {
            list.remove(index);
            textView.setTextColor(mContext.getResources().getColor(R.color.text_color6));
            textView.setBackgroundResource(R.drawable.bg_search_select_type_f);
        } else {
            if (maxSelect == 1) {
                if (list.size() > 0)
                    list.clear();
                for (TextView tv : textViewList) {
                    tv.setTextColor(mContext.getResources().getColor(R.color.text_color6));
                    tv.setBackgroundResource(R.drawable.bg_search_select_type_f);
                }
            }
            list.add(id);
            textView.setTextColor(mContext.getResources().getColor(R.color.white));
            textView.setBackgroundResource(R.drawable.bg_search_select_type_t);
        }

        if (listener != null)
            listener.onTypeChange();
    }


    public void setListener(TypeItemViewListener listener) {
        this.listener = listener;
    }

    public String getCateId() {
        String type = "";
        if (list != null && list.size() > 0) {
            for (int position : list) {
                type = type + position + ",";
            }
            if (type.length() > 1)
                type = type.substring(0, type.length() - 1);
        }
        return TextUtils.isEmpty(type) ? null : type;
    }

    public void clear() {
        for (TextView tv : textViewList) {
            tv.setTextColor(mContext.getResources().getColor(R.color.text_color6));
            tv.setBackgroundResource(R.drawable.bg_search_select_type_f);
        }
        if (list != null && list.size() > 0) list.clear();
        list = new ArrayList<>();
    }

    public boolean selectId(int selectId) {
        if (cateTypeModel == null || cateTypeModel.getCate_son() == null || cateTypeModel.getCate_son().size() == 0)
            return false;
        clear();
        int position = -1;
        for (int i = 0; i < cateTypeModel.getCate_son().size(); i++) {
            CateTypeModel.CateSonModel cateSonModel = cateTypeModel.getCate_son().get(i);
            if (cateSonModel == null) continue;
            if (cateSonModel.getCate_id() == selectId) {
                position = i;
            }
        }
        if (position == -1) return false;
        selectPoition(null, position);
        return true;
    }

}
